package huawei.year2017;

import java.util.Scanner;
import java.util.Stack;

/**
 * Created by Hyman on 2017/3/17.
 *
 * 后缀表达式求值
 * A~F，代表10~15
 * 运算符有+、-、*
 * 如postfixCal（"32+5-"）= 0
 */
public class PostFixCal {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            System.out.println(postfixCal(line));
        }
        scanner.close();
    }

    private static int postfixCal(String line) {

        Stack<Integer> stack = new Stack<Integer>();

        int len = line.length();
        int a, b;

        for (int i = 0; i < len; i++) {
            char c = line.charAt(i);
            if (Character.isDigit(c)) {
                stack.add(Integer.parseInt(String.valueOf(c)));
            } else if (String.valueOf(c).equals("+")) {

                a = stack.pop();
                b = stack.pop();

                stack.add(a + b);

            } else if (String.valueOf(c).equals("-")) {
                a = stack.pop();
                b = stack.pop();

                stack.add(b - a);
            } else if (String.valueOf(c).equals("*")) {
                a = stack.pop();
                b = stack.pop();

                stack.add(a * b);
            } else {
                stack.add(((int) c) - 55);
            }

        }

        return stack.peek();
    }
}
